Imports SchemaCodeGen

Partial Public Class pages_Contents_Content : Inherits CPage

#Region "Querystring"
	Public ReadOnly Property TemplateSetId() As Integer
		Get
			Return CWeb.RequestInt("setId")
		End Get
	End Property
	Public ReadOnly Property TemplateId() As Integer
		Get
			Return CWeb.RequestInt("templateId")
		End Get
	End Property
	Public ReadOnly Property IsEdit() As Boolean
		Get
			Return TemplateSetId <> Integer.MinValue AndAlso TemplateId <> Integer.MinValue
		End Get
	End Property
#End Region

#Region "Members"
	Private m_content As CContent
#End Region

#Region "Data"
	Public ReadOnly Property [Content]() As CContent
		Get
			If IsNothing(m_content) Then
				If IsEdit Then
					m_content = CContent.Cache.GetById(TemplateSetId, TemplateId)
					If IsNothing(m_content) Then CSitemap.RecordNotFound("Content", TemplateSetId, TemplateId)
				Else
					m_content = New CContent
					m_content.ContentTemplateId = TemplateId
					m_content.ContentSetId = TemplateSetId
				End If
			End If
			Return m_content
		End Get
	End Property
#End Region

#Region "Navigation"
	Private Sub Refresh()
		Response.Redirect(CSitemap.ContentEdit(Me.Content.ContentSetId, Me.Content.ContentTemplateId))
	End Sub
	Private Sub ReturnToList()
		Response.Redirect(CSitemap.ContentsForTemplateSet(TemplateSetId)) 'ContentsForTemplate(ContentTemplateId)
	End Sub
#End Region

#Region "Event Handlers - Page"
	Protected Overrides Sub PageInit()
		UnbindSideMenu()
		AddMenuSide("Search", CSitemap.Contents)

		'Page Title
		If IsEdit Then
			Me.Title = "Content Details"
			AddMenuSide(CUtilities.NameAndCount("Pattern", CTemplateSet.Cache), CSitemap.TemplateSetEdit(Content.ContentSetId), False, Content.TemplateSet.FullName)
			AddMenuSide(CUtilities.NameAndCount("Template", CTemplate.Cache), CSitemap.Template(Content.ContentTemplateId), False, Content.Template.TemplateName)
			AddMenuSide()
			AddMenuSide("New Content...", CSitemap.ContentsForTemplate(TemplateId))
		Else
			Me.Title = "Create New Content"
			If TemplateSetId > 0 Then AddMenuSide(CUtilities.NameAndCount("Pattern", CTemplateSet.Cache), CSitemap.TemplateSetEdit(TemplateSetId))
			If TemplateId > 0 Then AddMenuSide(CUtilities.NameAndCount("Template", CTemplate.Cache), CSitemap.Template(TemplateId))
			AddMenuSide("New Content...")
		End If


		'Button Text
		btnDelete.Visible = IsEdit
		If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Content"
	End Sub
	Protected Overrides Sub PageLoad()
		LoadContent()
	End Sub
#End Region

#Region "Event Handlers - Form"
	Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If Not Me.IsValid() Then Exit Sub
		SaveContent()
		Refresh()
	End Sub
	Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
		ReturnToList()
	End Sub
	Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
		Me.Content.Delete()
		'CCache.ClearCache()
		ReturnToList()
	End Sub
#End Region

#Region "Private - Load/Save"
	Protected Sub LoadContent()
		With Me.Content
			txtSet.Text = .TemplateSet.SetName_
			txtSet.NavigateUrl = CSitemap.TemplateSetEdit(.ContentSetId)

			txtTemplate.Text = .Template.TemplateName
			txtTemplate.NavigateUrl = CSitemap.Template(.ContentTemplateId)

			txtContentText.Text = .ContentText
			txtContentMD5.Text = CBinary.ToBase64(.ContentMD5, 8)

			txtLen.Text = .ContentText.Length.ToString("n0")


			txtContentCreated.Value = CUtilities.Timespan(.ContentCreated)
			txtContentModified.Value = CUtilities.Timespan(.ContentModified)
		End With
	End Sub
	Protected Sub SaveContent()
		With Me.Content
			.ContentText = txtContentText.Text

			.Save()
		End With
		'CCache.ClearCache()
	End Sub
#End Region

End Class
